<template>
  <view v-if="visible" class="tech-modal">
    <view class="modal-overlay" @click="$emit('close')"></view>
    <view class="modal-content">
      <view class="modal-header">
        <text class="modal-title">🔬 科技研究</text>
        <button class="close-btn" @click="$emit('close')">×</button>
      </view>
      <view class="tech-list">
        <view 
          class="tech-item" 
          v-for="(tech, index) in techs" 
          :key="index"
          :class="{ 
            'completed': tech.completed, 
            'researching': tech.researching,
            'can-research': !tech.completed && !tech.researching && canResearch(tech),
            'cannot-research': !tech.completed && !tech.researching && !canResearch(tech)
          }"
          @click="$emit('research', tech)"
        >
          <text class="tech-icon">{{ tech.icon }}</text>
          <text class="tech-name">{{ tech.name }}</text>
          <text class="tech-category">{{ tech.category }}</text>
          <text class="tech-description">{{ tech.description }}</text>
          <text class="tech-status">{{ tech.completed ? '已完成' : tech.researching ? '研究中' : '可研究' }}</text>
          <text class="tech-cost" v-if="!tech.completed">研究点数：{{ tech.cost }}</text>
          <view class="tech-effects" v-if="tech.effects && tech.effects.length > 0">
            <text class="effects-label">效果：</text>
            <text class="effects-items">{{ tech.effects.join('、') }}</text>
          </view>
          <view class="tech-prerequisites" v-if="!tech.completed && tech.prerequisites && tech.prerequisites.length > 0">
            <text class="prereq-label">前置条件：</text>
            <text class="prereq-items">{{ formatTechPrerequisites(tech.prerequisites) }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'TechModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    techs: {
      type: Array,
      default: () => []
    },
    researchPoints: {
      type: Number,
      default: 0
    }
  },
  methods: {
    // 检查是否可以研究
    canResearch(tech) {
      return this.checkTechPrerequisites(tech) && this.researchPoints >= tech.cost
    },
    
    // 检查科技前置条件
    checkTechPrerequisites(tech) {
      if (!tech.prerequisites || tech.prerequisites.length === 0) {
        return true
      }
      
      for (const prereqId of tech.prerequisites) {
        const prereqTech = this.techs.find(t => t.id === prereqId)
        if (!prereqTech || !prereqTech.completed) {
          return false
        }
      }
      return true
    },
    
    // 格式化科技前置条件
    formatTechPrerequisites(prerequisites) {
      if (!prerequisites || prerequisites.length === 0) return '无'
      return prerequisites.map(id => {
        const tech = this.techs.find(t => t.id === id)
        return tech ? tech.name : id
      }).join('、')
    }
  }
}
</script>

<style scoped>
/* 弹窗样式 */
.tech-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(5rpx);
}

.modal-content {
  position: relative;
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  border-radius: 20rpx;
  margin: 40rpx;
  width: calc(100% - 80rpx);
  height: calc(100% - 80rpx);
  max-width: 600rpx;
  max-height: 800rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1rpx solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.3);
}

.modal-header {
  padding: 30rpx;
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #f8fafc;
}

.close-btn {
  background: #ef4444;
  color: #fff;
  border: none;
  border-radius: 10rpx;
  padding: 8rpx 16rpx;
  font-size: 24rpx;
  cursor: pointer;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #dc2626;
  transform: scale(1.05);
}

/* 科技列表样式 */
.tech-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350rpx, 1fr));
  gap: 20rpx;
  padding: 20rpx;
  max-height: 70vh;
  overflow-y: auto;
}

.tech-item {
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  border: 2rpx solid #475569;
  border-radius: 16rpx;
  padding: 20rpx;
  text-align: left;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
  position: relative;
}

.tech-item:hover {
  transform: translateY(-4rpx);
  box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.3);
  border-color: #64748b;
}

.tech-item.completed {
  background: linear-gradient(135deg, #065f46 0%, #047857 100%);
  border-color: #10b981;
  box-shadow: 0 4rpx 12rpx rgba(16, 185, 129, 0.3);
}

.tech-item.researching {
  background: linear-gradient(135deg, #7c2d12 0%, #ea580c 100%);
  border-color: #f97316;
  box-shadow: 0 4rpx 12rpx rgba(249, 115, 22, 0.3);
  animation: pulse 2s ease-in-out infinite;
}

.tech-item.can-research {
  border-color: #3b82f6;
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
  box-shadow: 0 4rpx 12rpx rgba(59, 130, 246, 0.3);
}

.tech-item.cannot-research {
  opacity: 0.6;
  border-color: #ef4444;
  background: linear-gradient(135deg, #7f1d1d 0%, #ef4444 100%);
}

.tech-icon {
  font-size: 48rpx;
  display: block;
  margin-bottom: 12rpx;
  text-align: center;
}

.tech-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #f8fafc;
  display: block;
  margin-bottom: 8rpx;
  text-align: center;
}

.tech-category {
  font-size: 20rpx;
  color: #93c5fd;
  background: rgba(147, 197, 253, 0.1);
  padding: 4rpx 8rpx;
  border-radius: 8rpx;
  border: 1rpx solid rgba(147, 197, 253, 0.3);
  display: inline-block;
  margin-bottom: 8rpx;
  text-align: center;
  width: 100%;
}

.tech-description {
  font-size: 22rpx;
  color: #cbd5e1;
  display: block;
  margin-bottom: 8rpx;
  line-height: 1.4;
}

.tech-status {
  font-size: 24rpx;
  color: #94a3b8;
  display: block;
  text-align: center;
  margin-bottom: 8rpx;
}

.tech-cost {
  font-size: 20rpx;
  color: #fbbf24;
  display: block;
  text-align: center;
  margin-bottom: 8rpx;
  font-weight: bold;
}

.tech-effects {
  background: rgba(0, 0, 0, 0.3);
  border-radius: 8rpx;
  padding: 8rpx 12rpx;
  margin-bottom: 8rpx;
}

.effects-label {
  font-size: 20rpx;
  color: #10b981;
  font-weight: bold;
  display: block;
  margin-bottom: 4rpx;
}

.effects-items {
  font-size: 18rpx;
  color: #e5e7eb;
  display: block;
}

.tech-prerequisites {
  background: rgba(239, 68, 68, 0.2);
  border: 1rpx solid rgba(239, 68, 68, 0.3);
  border-radius: 8rpx;
  padding: 8rpx 12rpx;
}

.prereq-label {
  font-size: 20rpx;
  color: #ef4444;
  font-weight: bold;
  display: block;
  margin-bottom: 4rpx;
}

.prereq-items {
  font-size: 18rpx;
  color: #fca5a5;
  display: block;
}

/* 动画效果 */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}
</style>
